////////////////////////////////////////////////////////////////////////////////
//
// Aranya Code
// author: TonyHack
// E-mail: tonyjobmails@gmail.com
//
// URL格式：<协议>://<用户名>:<密码>@<主机域名|IP地址>:<端口号>/<文件目录> 
// URL类用于分解通用的URL，并判断其合法性
//
////////////////////////////////////////////////////////////////////////////////

#ifndef _URL_H
#define _URL_H

#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include "../aranya_base/type.h"
#include "../aranya_base/func.h"

/// URL长度最大值
#define MAX_URL_SIZE 256
/// 主机域名长度最大值64
#define MAX_HOST_SIZE 64
/// IP地址长度最大值16
#define MAX_IP_SIZE 16
/// HTTP默认端口
#define HTTP_PORT 80
/// FTP默认端口
#define FTP_PORT 21

using namespace std;
using namespace aranya_type;
/// 协议类型定义
enum
{
	SCHEME_HTTP = 0,
	SCHEME_FTP,
	SCHEME_FILE,
	SCHEME_OTHER
};

class URL
{
	public:
		//char url[MAX_URL_SIZE];
		string url;
		uint32 scheme;
		string username;
		string password;
		string host;
		uint32 port;
		string filename;
		string search_part;

		URL(const string& para_url_s) : url(para_url_s), username(""), password(""), host(""), port(0), filename(""), search_part(""), scheme(SCHEME_HTTP)
		{}

		URL(const char* para_url_c) : url(para_url_c) : url(para_url_s), username(""), password(""), host(""), port(0), filename(""), search_part(""), scheme(SCHEME_HTTP)
		{}
		URL() : url(""), username(""), password(""), host(""), port(0), filename(""), search_part(""), scheme(SCHEME_HTTP){}
		~URL(){}

		/// 设置url
		void set_url(const string& url)
		{
			this->url = url;
			this->scheme = SCHEME_HTTP;
			this->username = "";
			this->password = "";
			this->host = "";
			this->port = 0;
			this->filename = "";
			this->search_part = "";
		}

		/// 解析返回协议类型
		uint32 parse_scheme(const string &scheme_s);
		/// 解析url
		bool parse_url();

		///////////////////////////////////////////////////
		/// 测试
		void test_output()
		{
			using namespace std;
			cout << "URL: \"" << this->url << "\"的解析结果:" << endl;
			cout << "	协议: ";
			if(this->scheme == SCHEME_HTTP)
				cout << "http" << endl;
			else if(this->scheme == SCHEME_FTP)
				cout << "ftp" << endl;
			else if(this->scheme == SCHEME_FILE)
				cout << "file" << endl;
			else
				cout << "other unknown" << endl;
			cout << "	用户名: " << this->username << endl
			     << "	密码: " << this->password << endl
			     << "	主机: " << this->host << endl
			     << "	端口: " << this->port << endl
			     << "	文件路径: " << this->filename << endl
			     << "	查询请求: " << this->search_part << endl;
		}
		/// 测试结束
		///////////////////////////////////////////////////
};

#endif
